<?php
/**
 * キャンペーン取得用関数<br />
 *
 * @package   apamanshop
 * @author    SS山本
 * @copyright
 */
class Search_Model_Campaignsearch extends Apm_Model_Base
{

	/**
     * キャンペーン取得
     *
	 * @access	public
	 * @return  array   $result  取得結果
     */
	public function getCampaignsearch()
	{
		$sql = $this->generateCampaignSql();
		$result = $this->dbGetList($sql);

		return $result;
	}

    /**
     * キャンペーンSQL取得
     *
	 * @access	public
	 * @return  string
     */
	public function generateCampaignSql()
	{

		// 現在日取得
		$date = date('Ymd');

		$sql = "";
		$sql .= "SELECT ";
		$sql .= "   M94_CAMPAIGN_CD ";
		$sql .= "  ,M94_CAMPAIGN_RYAKUSHOU ";
		$sql .= "  ,M94_CAMPAIGN_URL ";
		$sql .= "  ,M94_CAMPAIGN_TOKUSYU_KBN ";
		$sql .= "FROM ";
		$sql .= "  M94CAMPAIGN ";
		$sql .= "WHERE ";
		$sql .= "      M94_CAMPAIGN_HYOUZI = 1 ";
		$sql .= "  AND M94_DELETE_FLAG = 0 ";
		$sql .= "  AND M94_CAMPAIGN_KBN IN (1,4) ";
		$sql .= "  AND M94_MOBILE_KBN = 0 ";
		//有効期間(開始がnullの場合は無効とする。終了がnullの場合はエンドレスとする)
		$sql .= "  AND " . $date . " BETWEEN NVL(M94_START_YMD, '99999999') AND NVL(M94_END_YMD, '99999999') ";
		//===並び順===
		$sql .= "ORDER BY ";
		$sql .= "   M94_CAMPAIGN_TOKUSYU_KBN ";
		$sql .= "  ,M94_CAMPAIGN_RANK ";
		$sql .= "  ,M94_CAMPAIGN_PRIORITY_NO ";
		$sql .= "  ,NVL(M94_START_YMD, '00000000') desc ";

		return $sql;
	}

	/**
	* キャンペーンデータを表示用に変換
	*
	* @access	public
	* @param	array
	* @param	array ※参照渡し
	* @return	void
	*/
	public function convertData($params, &$data)
	{
		$displist = array();

		foreach ($data['campaign'] as $listData)
		{
			$campaignId = $listData['M94_CAMPAIGN_CD'];
			$campaignChecked = "";

			// パラメータにキャンペーン条件がある場合,選択済みとする
			if (!empty($params['campaign']) && in_array($campaignId, $params['campaign']))
            {
            	$campaignChecked = "CHECKED";
            }
			$listData['CAMPAIGN_CHECKED'] = $campaignChecked;

			$displist[] = $listData;
		}

		$data['campaign'] = $displist;
	}

	/**
	* キャンペーンデータを表示用に変換（キャンペーン・特集に分ける）
	*
	* @access	public
	* @param	array
	* @param	array ※参照渡し
	* @return	void
	*/
	public function convertData2($params, &$data)
	{
		$displist = array();

		foreach ($data['campaign'] as $listData)
		{
			$tokusyuKbn = $listData['M94_CAMPAIGN_TOKUSYU_KBN'];
			// キャンペーン・特集区分が異なる場合
			if (array_key_exists($tokusyuKbn, $displist) == false)
			{
				$displist[$tokusyuKbn]['M94_CAMPAIGN_TOKUSYU_KBN'] = $tokusyuKbn;
			}

			$campaignId = $listData['M94_CAMPAIGN_CD'];
			$campaignChecked = "";

			// パラメータにキャンペーン条件がある場合,選択済みとする
			if (!empty($params['campaign']) && in_array($campaignId, $params['campaign']))
			{
				$campaignChecked = "CHECKED";
			}
			$listData['CAMPAIGN_CHECKED'] = $campaignChecked;

			$displist[$tokusyuKbn]['CAMPAIGN_DATA'][] = $listData;
		}

		$data['campaign'] = $displist;
	}

}
?>
